import tkinter
from tkinter import ttk
import tkinter as tk
#from IPython.terminal.pt_inputhooks import tk
from db import db

class InsertFrame(tkinter.Frame):
    #继承
    def __init__(self,root):
        super().__init__(master=root)
        self.username = tkinter.StringVar()
        self.math = tkinter.StringVar()
        self.chinese = tkinter.StringVar()
        self.english = tkinter.StringVar()
        self.status = tkinter.StringVar()
        self.creat_page()

    def creat_page(self):
        tkinter.Label(self,width=5,height=2).grid(row=0,column=0,padx=5,pady=5)
        tkinter.Label(self,text='姓名：').grid(row=1,column=0,padx=5,pady=5)
        tkinter.Entry(self,textvariable=self.username).grid(row=1,column=1,padx=5,pady=5)
        tkinter.Label(self,text='数学：').grid(row=2,column=0,padx=5,pady=5)
        tkinter.Entry(self,textvariable=self.math).grid(row=2,column=1,padx=5,pady=5)
        tkinter.Label(self,text='语文：').grid(row=3,column=0,padx=5,pady=5)
        tkinter.Entry(self,textvariable=self.chinese).grid(row=3,column=1,padx=5,pady=5)
        tkinter.Label(self,text='英语：').grid(row=4,column=0,padx=5,pady=5)
        tkinter.Entry(self,textvariable=self.english).grid(row=4,column=1,padx=5,pady=5)

        tkinter.Button(self,text='录入',command=self.recode).grid(row=5,column=1)
        tkinter.Label(self,textvariable=self.status).grid(row=6,column=1)
     
    def recode(self):
         print('记录数据事件！')
         print(
            self.username.get(),
            self.math.get(),
            self.chinese.get(),
            self.english.get(),
            self.status.get(),
         )
         stu = {
            'name':self.username.get(),
            'math':self.math.get(),
            'chinese':self.chinese.get(),
            'english':self.english.get(),
            'total':int(self.math.get())+int(self.chinese.get())+int(self.english.get()),
         }
         db.insert(stu)
         self.status.set('录入成功！')
         print(db.all())

class SearchFrame(tkinter.Frame):
    #继承
    def __init__(self,root):
        super().__init__(master=root)
        tkinter.Label(self,text="查询页面").pack()
        tkinter.Label(self,text="查询页面").pack()
        tkinter.Label(self,text="查询页面").pack()
        tkinter.Label(self,text="查询页面").pack()

class DeleteFrame(tkinter.Frame):
    #继承
    def __init__(self,root):
        super().__init__(master=root)
        self.status = tkinter.StringVar()
        tkinter.Label(self,text="请输入需删除信息的学生姓名：").pack()
        self.username = tkinter.StringVar()
        tkinter.Entry(self,textvariable=self.username).pack()
        tkinter.Button(self,text="删除",command=self.delete).pack()
    def delete(self):
        print('删除数据事件！')
        print(self.username.get())
        db.delete_by_name(self.username.get())
        self.status.set('删除成功！')
        print(db.all())




class AboutFrame(tkinter.Frame):
    #继承
    def __init__(self,root):
        super().__init__(master=root)
        tkinter.Label(self,text="版权所属Terry").pack()

class QueryFrame(tkinter.Frame):
    #继承
    def __init__(self,master=None):
        super().__init__(master)
        self.root = master
        self.itemName = tkinter.StringVar()

        self.table_frame = tkinter.Frame(self)
        self.table_frame.pack()
        self.row = 1
        self.creat_page()
    
    def creat_page(self):
        self.create_tree_view()
        self.show_data_frame()
        tkinter.Button(self,text="刷新数据",command=self.show_data_frame).pack(anchor=tkinter.E, pady=5)

    def show_data_frame(self):
        #删除原节点
        for _ in map(self.tree_view.delete,self.tree_view.get_children("")):
            pass
        students = db.all()
        for index, stu in enumerate(students):
            self.tree_view.insert("",index,values=(
                stu['name'],
                stu['math'],
                stu['chinese'],
                stu['english'],
                stu['total'],
            ))
        self.tree_view.pack()
    
    def create_tree_view(self):
        #表格
        colums = ('name','math','chinese','english','total')
        colums_value =  ('姓名','数学','语文','英语','总分')
        self.tree_view = tkinter.ttk.Treeview(self,columns=colums,show='headings')
        self.tree_view.column('name',width=100,anchor='center')
        self.tree_view.column('math',width=100,anchor='center')
        self.tree_view.column('chinese',width=100,anchor='center')
        self.tree_view.column('english',width=100,anchor='center')
        self.tree_view.column('total',width=100,anchor='center')
        self.tree_view.heading('name',text='姓名')
        self.tree_view.heading('math',text='数学')
        self.tree_view.heading('chinese',text='语文')
        self.tree_view.heading('english',text='英语')
        self.tree_view.heading('total',text='总分')
        self.tree_view.pack(fill=tkinter.BOTH,expand=True)



        